<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>图片上传预览</title>
<style>
	body{
		display: flex;
		width:100vw;
	}
	body img{
		width:500px;
		height:500px;
	}
	body input{
		display: block;
		padding:10px 20px;
		margin-left:20px;
		height:100px;
		align-self: center;
		outline: none;border:1px solid transparent;
		color:#fff;
		background: blue;
		border-radius:4px;		
	}	
</style>
<script type="text/javascript">
    function imgPreview(fileDom){
        //判断是否支持FileReader
        if (window.FileReader) {
            var reader = new FileReader();
        } else {
            alert("您的设备不支持图片预览功能，如需该功能请升级您的设备！");
        }

        //获取文件
        var file = fileDom.files[0];
        var imageType = /^image\//;
        //是否是图片
        if (!imageType.test(file.type)) {
            alert("请选择图片！");
            return;
        }
        //读取完成
        reader.onload = function(e) {
            //获取图片dom
            var img = document.getElementById("preview");
            //图片路径设置为读取的图片
            img.src = e.target.result;
        };
        reader.readAsDataURL(file);
    }
</script>

<script>
function showFileInput(){
    var fileInput=document.getElementById("fileInput");
    fileInput.click();
}
</script>
</head>
<body>
    <img id="preview" />
    <input type="file" style="display: none;" name="file" id="fileInput" onchange="imgPreview(this)" />
	<input id="img-upload" type="button"  onclick="showFileInput()" value="图片上传">
</body>
</html>
